package com.itheima.interceptor;

import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {
    @Override
    /**
     * handler拦截的处理对象
     * return决定是否放行,true放行
     */
    /*前置拦截*/
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor1..per"+handler);
        /*instanceof比对是否相同*/
        if (handler instanceof DefaultServletHttpRequestHandler){//对拦截器拦截的静态资源放行
            return true;
        }
        return false;
    }

    //后置拦截
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("post");
        //修改跳转的页面
        modelAndView.setViewName("main");
    }

    //页面执行完成之后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("after");
    }
}
